package org.fdy.common.core.model;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.fdy.common.core.enums.ResultCode;
import org.fdy.common.core.enums.ResultCodeTwo;
import org.fdy.common.core.exception.BaseException;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>说明：返回实体封装 </P>
 *
 * @author: fangdayang
 * @create: 2023-10-19 23:48
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResultTwo<T> implements Serializable {
    private static final long serialVersionUID = 722297166446265227L;
    /**
     * 响应code
     */
    private Integer code;

    /**
     * 响应描述
     */
    private String message;

    /**
     * 响应结果
     */
    private T data;


    public static ResultTwo<Boolean> result(ResultCodeTwo apiCode) {
        return result(apiCode, null);
    }

    public static <T> ResultTwo<T> result(ResultCodeTwo apiCode, T data) {
        return result(apiCode, null, data);
    }

    @SuppressWarnings("unchecked")
    public static <T> ResultTwo<T> result(ResultCodeTwo apiCode, String message, T data) {
        String apiMessage = apiCode.getMessage();
        if (StrUtil.isNotBlank(message)) {
            apiMessage = message;
        }
        return (ResultTwo<T>) ResultTwo.builder()
                .code(apiCode.getCode())
                .message(apiMessage)
                .data(data)
                .build();
    }


    public static <T> ResultTwo<T> ok() {
        return ok(null);
    }

    public static <T> ResultTwo<T> ok(T data) {
        return result(ResultCodeTwo.SUCCESS, data);
    }

    public static <T> ResultTwo<T> ok(T data, String message) {
        return result(ResultCodeTwo.SUCCESS, message, data);
    }

    public static ResultTwo<Map<String, Object>> okMap(String key, Object value) {
        Map<String, Object> map = new HashMap<>(1);
        map.put(key, value);
        return ok(map);
    }

    public static <T> ResultTwo<T> fail(ResultCodeTwo apiCode) {
        return result(apiCode, null);
    }

    public static <T> ResultTwo<T> fail(String message) {
        return result(ResultCodeTwo.FAIL, message, null);

    }

    public static <T> ResultTwo<T> fail(ResultCodeTwo apiCode, T data) {
        if (ResultCodeTwo.SUCCESS == apiCode) {
            throw new BaseException(ResultCode.FAIL.getCode(), "失败状态码不能为" + ResultCode.SUCCESS.getCode());
        }
        return result(apiCode, data);

    }

    public static <T> ResultTwo<T> fail(Integer errorCode, String message) {
        ResultTwo<T> tResult = new ResultTwo<>();
        tResult.setCode(errorCode);
        tResult.setMessage(message);
        return tResult;
    }
    public static ResultTwo<Map<String, Object>> fail(String key, Object value) {
        Map<String, Object> map = new HashMap<>(1);
        map.put(key, value);
        return result(ResultCodeTwo.FAIL, map);
    }

    public static <T> ResultTwo<T> fail() {
        return fail(ResultCodeTwo.FAIL);
    }
}
